folditfandomcom-20200222-history
Lua Scripting Tutorial (Advanced)
Introduction This tutorial is for more experienced programmers, giving some information about functions and tables. Functions If you already did a shake, wiggle or just printed out a text by script, you have already used (called) a function. But in addition to just using them, you can even create your own functions. Declaring a function, you need two things at first: A function name. This will allow us to call the function in the main program (or another function) by it's name when we need it. Function parameters (arguments). These are the variables, values (constants) or tables the function has to work with. In the declaration, they are put in braces after the function's name. If using several arguments in a function, they are separated by comma. print("Hello") Here, we call the function named print with only one argument - the constant expression "Hello". outputtext="Hello" print(outputtext) In this example, our print-function gets the variable outputtext as argument. Since now, we just used a defined function. It is time to create our own one: function print2(a,b) print("A contents:",a) print("B is:",b) end a="Hello" b="World" print2(a,b) Here, we defined the function print2 with two arguments, a and b. Inside our function, we call the function print twice, to show the content of our arguments. You can also see, that the print function also accepts more than one argument, and that we can use functions within functions. To remember, we could also call our function with constants: function print2(a,b) print("A contents:",a) print("B is:",b) end print2("Hello","World") This would give the same result. Now something about alias-names of variables within functions: function print2(c,d) print("A contents:",c) print("B is:",d) end a="Hello" b="World" print2(a,b) Compare the code with the second one before. We still feed the variables a and b as arguments into the function: a="Hello" b="World" print2(a,b) But in our function declaration, what was called a before is now called c, b is called d. The text to print is still the same: function print2(c,d) print("A contents:",c) print("B is:",d) end The possibility of using alias-names has some advantages: You don't need to change variable names in any function sections, when you: write a new program and decide to use other variable names than before in the code which is calling the function copy a function from a user who works with other variable names call the function more than one time in a code, working on different variables each call But take care! Aliasing only works on variables, if they are submitted as arguments in a function call by their initial names and in the argument list of the function declaration by their alias-names! If a variable name within a function is not mentioned as argument at function begin, it is a global variable. If the keyword local is even put before it, it is a local variable. The expressions global and local describe the scope of variables. In our example before, you can say that c has a local content of a, and d has a local content of b. Local, because the values of c and d only exist within the function, outside those variables don't have these values. Outside the function the values are still stored in the variables a and b. Let's see this in a smaller example, at first with a local variable: function test() local a=5 print ("A at function call is:",a) end print("A before function call is:",a) test () print("A after function call is:",a) This should result: A before function call is: nil A at function call is: 5 A after function call is: nil You can see, that outside the function a is nil, as we didn't set a value there for it. Inside the function, we set it to 5. When leaving the function, a is nil again. Let's see what happens if we remove the local expression, so we use a global variable: function test() a=5 print ("A at function call is:",a) end print("A before function call is:",a) test () print("A after function call is:",a) This should result: A before function call is: nil A at function call is: 5 A after function call is: 5 ... to be continued... Tables Tables are like drawers of a cupboard. They can contain a set of variables, values or other tables, you can even mix these different contents. ... to be continued... Reference For the original source of information and other examples, go to this address: Lua 5.1 Reference Manual Category:script tutorial